package graduate.cslg.com.universityshopping.dormitory;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Toolbar;

import org.slf4j.MDC;

import graduate.cslg.com.universityshopping.R;
import graduate.cslg.com.universityshopping.net.MyDorBean;
import graduate.cslg.com.universityshopping.net.RetrofitDevelop;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class MyDorActivity extends AppCompatActivity {

    private TextView stuId;
    private TextView stuSex;
    private TextView stuDepartment;
    private TextView stuType;
    private TextView stuGrade;
    private TextView stuBed;
    private TextView stuNation;
    private TextView stuTel;
    private MyDorBean mDorBean;
    private String getUserAccount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_dor);
        SharedPreferences preferences = this.getSharedPreferences("user_info",MODE_PRIVATE);
        getUserAccount = preferences.getString("user_account", "Z09315222");
        initViews();
        setInfo(getUserAccount);
    }

    private void initViews() {
        stuId = findViewById(R.id.stu_id);
        stuSex = findViewById(R.id.stu_sex);
        stuDepartment = findViewById(R.id.stu_department);
        stuType = findViewById(R.id.stu_type);
        stuGrade = findViewById(R.id.stu_grade);
        stuBed = findViewById(R.id.stu_place);
        stuNation = findViewById(R.id.stu_nation);
        stuTel = findViewById(R.id.stu_tel);
    }

    private void setInfo(String stuAccount) {
        RetrofitDevelop.getTestService()
                .getDocInfo(stuAccount)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<MyDorBean>() {
                    @Override
                    public void onCompleted() {
                        stuId.setText(mDorBean.getStu_account());
                        stuSex.setText(mDorBean.getStu_sex());
                        stuDepartment.setText(mDorBean.getStu_department());
                        stuType.setText(mDorBean.getStu_type());
                        stuGrade.setText(String.valueOf(mDorBean.getStu_grade()));
                        stuBed.setText(mDorBean.getStu_dorinfo());
                        stuNation.setText(mDorBean.getStu_nation());
                        stuTel.setText(mDorBean.getStu_tel());
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        Toast.makeText(MyDorActivity.this, "错误，请稍候再试！", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onNext(MyDorBean myDorBean) {
                        mDorBean = myDorBean;
                    }
                });
    }
}
